package com.aijk.commons.crud.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.crud.dao.BaseMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import java.util.List;

/**
 * Created by iscdw on 2016/3/13.
 */
public abstract class AbstractService<BEAN, EXAMPLE,IDTYPE> {

    private BaseMapper<BEAN, EXAMPLE,IDTYPE> baseMapper;

    public int countByExample(EXAMPLE example) {
        return getDao().countByExample(example);
    }

    public int deleteByExample(EXAMPLE example) {
        return getDao().deleteByExample(example);
    }

    public int deleteByPrimaryKey(IDTYPE id) {
        return getDao().deleteByPrimaryKey(id);
    }

    public int insert(BEAN record) {
        return getDao().insert(record);
    }

    public int insertSelective(BEAN record) {
        return getDao().insertSelective(record);
    }

    public List<BEAN> selectByExample(EXAMPLE example) {
        return getDao().selectByExample(example);
    }

    public BEAN selectByPrimaryKey(IDTYPE id) {
        return (BEAN) getDao().selectByPrimaryKey(id);
    }

    public int updateByExampleSelective(BEAN record, EXAMPLE example) {
        return getDao().updateByExampleSelective(record, example);
    }

    public int updateByExample(BEAN record, EXAMPLE example) {
        return getDao().updateByExample(record, example);
    }

    public int updateByPrimaryKeySelective(BEAN record) {
        return getDao().updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(BEAN record) {
        return getDao().updateByPrimaryKey(record);
    }

    public PageInfo<BEAN> getPageInfo(PagingRequest pagingRequest, BEAN queryBean) {
        EXAMPLE example = getPageExample(queryBean);
        PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
        return new PageInfo(getDao().selectByExample(example));
    }

    /**
     * 获得Mapper
     *
     * @return
     */
    public abstract BaseMapper getDao();

    /**
     * 分页查询条件
     *
     * @return
     */
    public abstract EXAMPLE getPageExample(BEAN queryBean);
}
